package com.jyh.service.money;


import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.jyh.config.BaseAction;
import com.jyh.config.datasource.TargetDataSource;
import com.jyh.db.entity.JyhBankList;
import com.jyh.db.entity.JyhBankListExample;
import com.jyh.db.entity.JyhConfig;
import com.jyh.db.entity.JyhConfigExample;
import com.jyh.db.mapper.JyhBankListMapper;
import com.jyh.db.mapper.JyhConfigMapper;
import com.jyh.dbOrder.dao.LoanDao;
import com.jyh.service.ButtonService;
import com.jyh.util.StringUtils;

@Service
public class RechargeService extends BaseAction {
	@Autowired
	private LoanDao loanDao; 
	@Autowired
	private JyhConfigMapper configMapper;
	@Autowired
	private JyhBankListMapper bankListMapper;
	@Autowired
	private ButtonService buttonService;
	/**
	 * 获取充值页面数据
	 * @param request
	 * @param session
	 * @return
	 */
	@TargetDataSource(name = "order")
	public ModelAndView getRecharge(HttpServletRequest request, HttpSession session) {
		ModelAndView mv = new ModelAndView("");
		String rechargeState  = request.getParameter("rechargeState");
		String sqls  = "";
		if(StringUtils.isNotNull(rechargeState) && rechargeState.equals("1")){
			mv = new ModelAndView("money/inRecharge");
			sqls  = " order by t1.submitTime desc";
		}else if(StringUtils.isNotNull(rechargeState) && rechargeState.equals("2")){
			mv = new ModelAndView("money/rechargeSuccess");
			sqls  = " order by t1.successTime desc";
		}else if(StringUtils.isNotNull(rechargeState) && rechargeState.equals("3")){
			mv = new ModelAndView("money/rechargeFail");
			sqls  = " order by t1.failTime desc";
		}
		String userNO  = request.getParameter("userNO");
		String orderNO  = request.getParameter("orderNO");
		String userName  = request.getParameter("userName");
		String channel  = request.getParameter("channel");
		String bank  = request.getParameter("bank");
		String rechargeType  = request.getParameter("rechargeType");
		String source  = request.getParameter("source");
		String moneyNO  = request.getParameter("moneyNO");
		String submitTime1  = request.getParameter("submitTime1");
		String submitTime2  = request.getParameter("submitTime2");
		String successTime1  = request.getParameter("successTime1");
		String successTime2  = request.getParameter("successTime2");
		String failTime1  = request.getParameter("failTime1");
		String failTime2  = request.getParameter("failTime2");
		
		String page = request.getParameter("currentPage");// 传过来的页数
		int lineSize = 20; // 每页显示记录20条数
		int pageCount = 0; // 总页数
		int count = 0; // 数据总数
		int currentPage = 1; // 当前是第几页,默认是第一页
		if (page == null || "1".equals(page)) {
		} else {
			currentPage = Integer.parseInt(page); // 当前是第几页,默认是第一页
		}
		if (currentPage < 1) {
			currentPage = 1;
		}
		String limitSql = " limit " + (currentPage - 1) * lineSize + "," + lineSize;
		String sql1 = "select count(1) as count from jyh_user_recharge_log t1 "
				+ " left join (select * from jyh_config where groups='source') t2 on t1.source=t2.value "
				+ " left join (select * from jyh_config where groups='rechargeType') t3 on t1.rechargeType=t3.value where 1=1";
		
		String sql = "select t1.*,t2.valName sourceName,t3.valName rechargeTypeName from jyh_user_recharge_log t1 "
				+ " left join (select * from jyh_config where groups='source') t2 on t1.source=t2.value "
				+ " left join (select * from jyh_config where groups='rechargeType') t3 on t1.rechargeType=t3.value where 1=1";
		if (StringUtils.isNotNull(userNO)) {
			sql = sql + " and t1.userNO='" + userNO + "'";
			sql1 = sql1 + " and t1.userNO='" + userNO + "'";
		}
		if (StringUtils.isNotNull(orderNO)) {
			sql = sql + " and t1.orderNO='" + orderNO + "'";
			sql1 = sql1 + " and t1.orderNO='" + orderNO + "'";
		}
		if (StringUtils.isNotNull(userName)) {
			sql = sql + " and t1.userName<='" + userName + "'";
			sql1 = sql1 + " and t1.userName<='" + userName + "'";
		}
		if (StringUtils.isNotNull(channel)) {
			sql = sql + " and t1.channel='" + channel + "'";
			sql1 = sql1 + " and t1.channel='" + channel + "'";
		}
		if (StringUtils.isNotNull(bank)) {
			sql = sql + " and t1.bankName='" + bank + "'";
			sql1 = sql1 + " and t1.bankName='" + bank + "'";
		}
		if (StringUtils.isNotNull(moneyNO)) {
			sql = sql + " and t1.moneyNO='" + moneyNO + "'";
			sql1 = sql1 + " and t1.moneyNO='" + moneyNO + "'";
		}
		if (StringUtils.isNotNull(submitTime1)) {
			sql = sql + " and t1.submitTime>='" + submitTime1 + "'";
			sql1 = sql1 + " and t1.submitTime>='" + submitTime1 + "'";
		}
		if (StringUtils.isNotNull(submitTime2)) {
			sql = sql + " and t1.submitTime<='" + submitTime2 + "'";
			sql1 = sql1 + " and t1.submitTime<='" + submitTime2 + "'";
		}
		if (StringUtils.isNotNull(rechargeState)) {
			sql = sql + " and t1.rechargeState='" + rechargeState + "'";
			sql1 = sql1 + " and t1.rechargeState='" + rechargeState + "'";
		}
		if (StringUtils.isNotNull(rechargeType)) {
			sql = sql + " and t1.rechargeType='" + rechargeType + "'";
			sql1 = sql1 + " and t1.rechargeType='" + rechargeType + "'";
		}
		if (StringUtils.isNotNull(source)) {
			sql = sql + " and t1.source='" + source + "'";
			sql1 = sql1 + " and t1.source='" + source + "'";
		}
		if (StringUtils.isNotNull(successTime1)) {
			sql = sql + " and t1.successTime>='" + successTime1 + "'";
			sql1 = sql1 + " and t1.successTime>='" + successTime1 + "'";
		}
		if (StringUtils.isNotNull(successTime2)) {
			sql = sql + " and t1.successTime<='" + successTime2 + "'";
			sql1 = sql1 + " and t1.successTime<='" + successTime2 + "'";
		}
		if (StringUtils.isNotNull(failTime1)) {
			sql = sql + " and t1.failTime>='" + failTime1 + "'";
			sql1 = sql1 + " and t1.failTime>='" + failTime1 + "'";
		}
		if (StringUtils.isNotNull(failTime2)) {
			sql = sql + " and t1.failTime<='" + failTime2 + "'";
			sql1 = sql1 + " and t1.failTime<='" + failTime2 + "'";
		}
		
		Map<String, Object> map = loanDao.getMap(sql1);
		count = Integer.parseInt(map.get("count") + "");
		if(count<=lineSize){
			limitSql = " limit 0," + lineSize;
		}
		sql = sql + sqls + limitSql;
		List<Map<String, Object>> list = loanDao.getList(sql);
		
		int temp = count / lineSize; // 取商
		int tem2 = count % lineSize;// 取余数
		if (tem2 > 0 && tem2 < lineSize && temp > 0) {
			pageCount = count / lineSize + 1; // 总页数
		} else if (tem2 == 0 && temp > 0) {
			pageCount = temp;
		} else {
			pageCount = 1; // 总页数
		}
		if (currentPage >= pageCount) {
			currentPage = pageCount;
		}
		
		// 获取充值来源
		JyhConfigExample example = new JyhConfigExample();
		example.createCriteria().andGroupsEqualTo("source");
		List<JyhConfig> sourceList = configMapper.selectByExample(example);
//		logger.info("获取充值来源sourceList={}",JSONObject.toJSONString(sourceList));
		
		// 获取充值渠道
		JyhConfigExample jyhConfigExample = new JyhConfigExample();
		jyhConfigExample.createCriteria().andGroupsEqualTo("channel");
		List<JyhConfig> channelList = configMapper.selectByExample(jyhConfigExample);
//		logger.info("获取充值渠道channelList={}",JSONObject.toJSONString(channelList));
		
		// 获取充值方式
		JyhConfigExample configExample = new JyhConfigExample();
		configExample.createCriteria().andGroupsEqualTo("rechargeType");
		List<JyhConfig> rechargeTypeList = configMapper.selectByExample(configExample);
//		logger.info("获取充值方式rechargeTypeList={}",JSONObject.toJSONString(rechargeTypeList));
		
		list = (List<Map<String, Object>>) buttonService.getTableButton(27, "orderNO", jsonOutSimplify(list), session);
		mv.addObject("list", list);
		mv.addObject("sourceList", sourceList);
		mv.addObject("channelList", channelList);
		mv.addObject("rechargeTypeList", rechargeTypeList);
		mv.addObject("pageCount", pageCount);
		mv.addObject("currentPage", currentPage);
		mv.addObject("count", count);
		//将传递过来的参数回显
		mv.addObject("moneyNO", moneyNO);
		mv.addObject("submitTime1", submitTime1);
		mv.addObject("submitTime2", submitTime2);
		mv.addObject("userNO", userNO);
		mv.addObject("orderNO", orderNO);
		mv.addObject("userName", userName);
		mv.addObject("successTime1", successTime1);
		mv.addObject("successTime2", successTime2);
		mv.addObject("source", source);
		mv.addObject("rechargeType", rechargeType);
		mv.addObject("bank", bank);
		mv.addObject("channel", channel);
		return mv;
	}
	
	
	/**
	 * 获取页面筛选--提现银行
	 * @param request
	 * @param session
	 * @return
	 */
	@TargetDataSource(name = "user")
	public ModelAndView getSelectData(ModelAndView mv) {
		// 获取提现银行
		List<JyhBankList> bankList = bankListMapper.selectByExample(new JyhBankListExample());
		mv.addObject("bankList", bankList);
//		logger.info("获取提现银行bankList={}",JSONObject.toJSONString(bankList));
		return mv;
	}
	
	/**
	 * 重新代扣
	 * @param request
	 * @param session
	 * @return
	 */
	@TargetDataSource(name = "order")
	public ModelAndView getReWithholding(HttpServletRequest request, HttpSession session) {
		ModelAndView mv = new ModelAndView("");
		
		mv.addObject("list", null);
		return mv;
	}
	
	
}
